﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevTeam;

namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class TopBolmeIptal : XPopupForm
    {
        private int CikisEmriId;
        private string SelectedProductIds = "",
                          SelectedRollIds = "";
        public TopBolmeIptal(int CikisEmriId, string SelectedProductIds, string SelectedRollIds)
        {
            this.CikisEmriId = CikisEmriId;
            this.SelectedProductIds = SelectedProductIds;
            this.SelectedRollIds = SelectedRollIds;
            InitializeComponent();
        }

        private void TopBolmeIptal_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetBolunenTopForCikisEmriIptal", CikisEmriId, SelectedProductIds, SelectedRollIds);
            //Type t1 = new Type();
            //GridManager gm = gridView1.GetManager();
            //DataTable table = new DataTable();
            //table.Columns.Add("RollId");
            //table.Columns.Add("ProductId");
            //table.Columns.Add("TopBolundu");
            //gm.DataTable = BolunenToplar;

            gridView1.MakeReadOnly("TopBolundu");

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView1.RowCellClick += gridView1_RowCellClick;
        }

        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName == "TopBolundu")
                {
                    bool currentStatus = false;
                    if (e.CellValue != DBNull.Value)
                    {
                        currentStatus = (bool)e.CellValue;
                    }
                    gridView1.SetRowCellValue(e.RowHandle, e.Column, !currentStatus);
                }

            }
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtIptalNedeni.EditValue.ToStr().Equals(String.Empty))
            {
                XtraMessageBox.Show("İptal Nedenini Girmeniz Zorunludur!", "Bilgi");
                return; 
            }
            else
            {
                gridView1.EndEditing();
                DialogResult = DialogResult.OK;
            }
        }

        public String GetBolunenRollIdler()
        {
            int j = 0;
            String BolunenRollIdler = "";
            DataTable table = gridView1.DataTable();
            for (int i = 0; i < table.Rows.Count; ++i)
            {
                if (table.Rows[i]["TopBolundu"].ToBool() == false)
                {
                    if (j == 0)
                        BolunenRollIdler = table.Rows[i]["BolunenTopId"].ToStr();
                    else
                        BolunenRollIdler += "," + table.Rows[i]["BolunenTopId"].ToStr();

                    ++j;
                }
            }

            return BolunenRollIdler;
        }

        public String GetBolunmusRollIdler()
        {
            int j = 0;
            String BolunmusRollIdler = "";
            DataTable table = gridView1.DataTable();
            for (int i = 0; i < table.Rows.Count; ++i)
            {
                if (table.Rows[i]["TopBolundu"].ToBool() == true)
                {
                    if (j == 0)
                        BolunmusRollIdler = table.Rows[i]["BolunenTopId"].ToStr();
                    else
                        BolunmusRollIdler += "," + table.Rows[i]["BolunenTopId"].ToStr();

                    ++j;
                }
            }

            return BolunmusRollIdler;
        }

        public String GetEditValue()
        {
            return txtIptalNedeni.EditValue.ToStr();
        }
    }
}
